@extends('layouts.app')

@section('title', translate('购物车'))
@section('content')
@include('common.header', ['title'=> translate('购物车')])
<link href="/fontpage/css/weixin.css" rel="stylesheet" type="text/css">
<link href="/fontpage/css/ionic.min.css" rel="stylesheet" type="text/css">
<link href="/fontpage/css/signin.css" rel="stylesheet" type="text/css">
<link href="/fontpage/css/share.css" rel="stylesheet" type="text/css">


<script type="text/javascript" src="/fontpage/js/ionic.bundle.min.js"></script>
<script type="text/javascript" src="/fontpage/js/runend.js"></script>
<script type="text/javascript">
        $(function () {
            if (true) {
                $("#f4").html('<img src="/fontpage/images/gouwuche1.png" width="30" height="30">');
                $("#t4").css("color", "#ee4b02");
            }
        });

        //定义全局变量
        var i = 0;

        //金额总和
        var money = 0;

        //计算合计价格
        var cart_money = new Object();

        //全部商品ID
        var cart_id = new Object();
        //备份商品ID，用于全选后去掉全选又再次全选
        var cart_id_copy = new Object();

    </script>
<form method="post" name="cart_form" target="_self" id="cart_form" action="">
    <!--list-->
    <div class="commodity_list_box">
        <div class="cart_top">
            <span>{{translate('桔子屋商品')}}</span>
            <div class="clear"></div>
        </div>
        <!--商品列表-->
        <div class="commodity_box">
            <div class="commodity_list">
                <!--商品-->
                <ul class="commodity_list_term">

                    @foreach($pageList as $row)
                        <li class="select">
                            <!--  -->

                                <em aem="0" cart_id="{{$row->id}}" sub="true"></em>
                            <img src="{{url('upload/'.$row->goods->thumb)}}">
                            <div class="div_center">

                                    <h4>{{translate($row->goods, 'title')}}</h4>
                                    <span>{{$row->number}}{{translate($row->goods_attr, 'unit')}}</span>
                                    <p class="now_value"><i>￥ </i><b class="qu_su">{{$row->goods_attr->price}}</b></p>


                            </div>
                            <div class="div_right">
                                <input type="hidden" value="{{$row->id}}">
                            </div>

                        </li>
                    @endforeach


                </ul>
            </div>
        </div>
        <!-- 商品列表 end -->
    </div>
    <!-- end -->
    <div style="width: 100%;height:17vh;"></div>
    <!-- footer -->
    <div class="settle_box">
        <dl class="all_check select">
            <dt><span id="all_pitch_on"></span>
                <div style="color:#ff900d;padding-left:8px;display: inline-block;font-size: 16px;">{{translate('全选')}}</div>
            </dt>
        </dl>
        <dl class="total_amount">
            <dt>{{translate('合计')}}：<p id="total_price">¥<b>0</b></p></dt>
            <dd>{{translate('不含运费')}}</dd>
        </dl>
        <input type="hidden" name="gcs" id="gcs">
        <a class="settle_btn" id="confirm_cart">{{translate('结算')}}</a>
        <a class="settle_btn" href="javascript:void(0);" id="confirm_cart1" onclick="big_cart_remove()">{{translate('删除')}}</a>
    </div>


</form>

<script>

    /* 编辑商品  */
    var topb = 0;
    $("#rem_s").click(function () {
        if (topb == 0) {
            $(this).text("完成");
            $(".total_amount").hide();
            /* 合计  */
            $("#confirm_cart").hide();
            /* 结算  */
            $("#confirm_cart1").show();
            /* 删除 */
            topb = 1;
        } else {
            topb = 0;
            $(this).text("编辑");
            $(".total_amount").show();
            /* 合计  */
            $("#confirm_cart").show();
            /* 结算  */
            $("#confirm_cart1").hide();
            /* 删除 */
            allThis.removeClass("pitch_on");
            /* 取消所有选择  */
            $("#all_pitch_on").removeClass("pitch_on");
            /* 取消所有选择  */
            $("#total_price b").text("0");
            /*合计价格清零*/
        }

    });

    //选择结算商品
    $(".select em").click(function () {
        if ($(this).hasClass("pitch_on"))
        {
            $(this).removeClass("pitch_on");
            Cal();
        }
        else
        {
            $(this).addClass("pitch_on");
            Cal();
        }



    })

    /* 底部全选  */
    $("#all_pitch_on").click(function () {
        if($(this).hasClass("pitch_on"))
        {
            $(".select em").removeClass("pitch_on");
            $(this).removeClass("pitch_on");
            Cal();
        }
        else
        {
            $(".select em").addClass("pitch_on");
            $(this).addClass("pitch_on");
            Cal();
        }


    });

    function Cal() {
        var all = $(".select em").length;   //物品个数
        var $elements = $(".select").children(".pitch_on:not([sub='false'])");
        var len = $elements.length;
        if(all==len && all > 0)
        {
            $("#all_pitch_on").addClass("pitch_on");
        }
        else if(len < all)
        {
            $("#all_pitch_on").removeClass("pitch_on");
        }
        //console.log(all);
        var json = new Array();
        if(len>0)
        {
            $elements.each(function(i) {
                //var $this = $(this);
                //alert($this.prop('tagName'));
            	json[i] = $(this).attr("cart_id")
            });
            console.log(json);
            ajaxurl("{{JpUrl('/user/getCal')}}",{carts:json}, function(data){
            	if(data.status=="1")
                {
                    $("#total_price").text("￥ "+data.total);
                }
                else
                {
                    $("#total_price").text("￥ "+0);
                }

             })

        }
        else if(len==0)
        {
            $("#total_price").text("￥ "+0);
        }

        //$(".total_price").text(len);

    }

    //删除
    function big_cart_remove() {
        var $elements = $(".select").children(".pitch_on");
        var len=$elements.length;

        var json = {};
        json.ids = new Array();
        json.type = 'del';
        if(len>0)
        {
            $elements.each(function(i) {
            	json.ids[i] = $(this).attr("cart_id")
            });
			ajaxurl('', json, function(data){
				console.log(data);
                if(data.status == "1")
                {
                    $(".commodity_list_term .pitch_on").parent().remove();
                    $(".commodity_list .tite_tim > em.pitch_on").parents(".commodity_box").remove();
                }
                else
                {
                    window.location.reload();
                }
			});

        }
        else
        {

        }


    }

    //结算
    $("#confirm_cart").click(function () {
        var $elements = $(".select").children(".pitch_on:not([sub='false'])");
        var len=$elements.length;
        if( len>0 ) {
            var json = new Array();
            $elements.each(function (i) {
                //var $this = $(this);
                json[i] = $(this).attr("cart_id");
            });
            //提交购物车
            var form = $('<form>{{ csrf_field() }}</form>');
            form.attr('action', "{{JpUrl('/user/cartCommit')}}");
            form.attr('method', 'post');
            form.attr('target', '_self');

            var input1 = $("<input type='hidden' name='json' />");
            input1.attr('value', json);
            form.append(input1);

            var input2 = $("<input type='hidden' name='type' />");
            input2.attr('value', 1);
            form.append(input2);

            form.appendTo("body");
            form.css('display', 'none');
            form.submit();
        }
        else
        {
            layer.alert({{translate("请选择商品")}});
        }

    });

</script>
@endsection
@section('footer')
     @include('common.footer')
@stop
